﻿using System.Windows;
using GameEngine.Clients.Silverlight;
using SilverlightShowCase.Commands;
using SilverlightShowCase.View.Components;
using GameEngine.Clients.Silverlight.View.Components;

namespace SilverlightShowCase
{
    public class MyFacade : SilverlightFacade<EditorMyGameUC, EditorLevelUC>
    {
        public const string MyMainMenuMediatorName = "MainMenuMediatorName";
        public const string MySelectGameMenuMediatorName = "SelectGameMenuMediatorName";
        public const string MyGameMainMenuMediatorName = "GameMainMenuMediatorName";
        public const string MyGamesEditorMenuMediatorName = "GamesEditorMenuMediatorName";
        public const string EditorEditPageMediatorName = "EditorEditPageMediatorName";

        public const string MY_PAGE_MAIN_IN_COMMAND = "MY_PAGE_MAIN_IN_COMMAND";
        public const string MY_PAGE_MAIN_OUT_COMMAND = "MY_PAGE_MAIN_OUT_COMMAND";

        public const string MY_PAGE_PLAY_IN_COMMAND = "MY_PAGE_PLAY_IN_COMMAND";
        public const string MY_PAGE_PLAY_OUT_COMMAND = "MY_PAGE_PLAY_OUT_COMMAND";

        public const string MY_PAGE_GAMESEDITOR_IN_COMMAND = "MY_PAGE_GAMESEDITOR_IN_COMMAND";
        public const string MY_PAGE_GAMESEDITOR_OUT_COMMAND = "MY_PAGE_GAMESEDITOR_OUT_COMMAND";

        public const string PAGE_EDITOREDITGAME_IN_COMMAND = "PAGE_EDITOREDITGAME_IN_COMMAND";
        public const string PAGE_EDITOREDITGAME_OUT_COMMAND = "PAGE_EDITOREDITGAME_OUT_COMMAND";
        
        // Override Singleton Factory method 
        public new static MyFacade Instance
        {
            get
            {
                return SilverlightFacade<GameUC, LevelUC>.Instance as MyFacade;
            }
        }

        protected override void InitializeController()
        {
            base.InitializeController();

            RegisterCommand(FPS_NOTIFICATION, typeof(FPSCommand));//TODO meter isto como evento

            RegisterCommand(MY_PAGE_MAIN_IN_COMMAND, typeof(Page_Main_In_Command));
            RegisterCommand(MY_PAGE_MAIN_OUT_COMMAND, typeof(Page_Main_Out_Command));

            RegisterCommand(MY_PAGE_PLAY_IN_COMMAND, typeof(Page_GamePlay_In_Command));
            RegisterCommand(MY_PAGE_PLAY_OUT_COMMAND, typeof(Page_GamePlay_Out_Command));

            RegisterCommand(MY_PAGE_GAMESEDITOR_IN_COMMAND, typeof(Page_GamesEditor_In_Command));
            RegisterCommand(MY_PAGE_GAMESEDITOR_OUT_COMMAND, typeof(Page_GamesEditor_Out_Command));
        }

        override protected void CreateModelFacade()
        {
            new MyModelFacade();
        }

        protected override void CreateViewFacade()
        {
            new MyViewFacade();
        }
    }

    public class MyViewFacade : SilverlightViewFacade<EditorMyGameUC, EditorLevelUC> {

        public override EditorMyGameUC CreateGameView()
        {
            return new EditorMyGameUC();
        }

        public override EditorLevelUC CreateLevelView()
        {
            return new EditorLevelUC();
        }
    }

    public class MyFacadeHelper : SilverlightApplicationFacadeHelper
    {
        public static void Start(Application app)
        {
            MyFacade.Instance.SendNotification(MyFacade.MY_PAGE_MAIN_IN_COMMAND, app);
        }
    }

    public class MyModelFacade : SilverlightModelFacade
    {

        //public override GameEngine.Model.IThingDataProxy<int> CreateThingDataProxy(IThingVO<int> vo)
        //{
        //    if (vo.GetType().GUID == typeof(PeasantVO).GUID)
        //    {
        //        return new PeasantDataProxy(vo as PeasantVO);
        //    }
        //    else if (vo.GetType().GUID == typeof(BuildingVO).GUID)
        //    {
        //        return new BuildingDataProxy(vo as BuildingVO);
        //    }
        //    return base.CreateThingDataProxy(vo);
        //}

        //public override IThingTypeDataProxy<int> CreateThingTypeDataProxy(IThingTypeVO<int> vo)
        //{
        //    if (vo.GetType().GUID == typeof(PeasantTypeVO).GUID)
        //    {
        //        return new PeasantTypeDataProxy(
        //                vo as PeasantTypeVO,
        //                CreateThingTypeViewDataProxy(Game.Views[vo.ViewId]));
        //    }
        //    else if (vo.GetType().GUID == typeof(BuildingTypeVO).GUID)
        //    {
        //        return new BuildingTypeDataProxy(
        //                vo as BuildingTypeVO,
        //                CreateThingTypeViewDataProxy(Game.Views[vo.ViewId]));
        //    }
        //    return base.CreateThingTypeDataProxy(vo);
        //}

        //public override IThingVO<int> CreateThingVO(IThingVOId<int> id, IThingTypeVOId typeId)
        //{
        //    if (Game.ThingTypes[typeId].GetType().GUID == typeof(PeasantTypeVO).GUID)
        //    {
        //        return new PeasantVO(id, typeId);
        //    }
        //    else if (Game.ThingTypes[typeId].GetType().GUID == typeof(BuildingTypeVO).GUID)
        //    {
        //        return new BuildingVO(id, typeId);
        //    }
        //    return base.CreateThingVO(id, typeId);
        //}

        //public override void InitializeWorldActions()
        //{
        //    base.InitializeWorldActions();
        //    EngineDataProxy<int>.Instance.AddWorldAction(
        //           new EngineDataProxy<int>.WorldAction(
        //               typeof(BuildingDataProxy),
        //               BuildingDataProxy.Action,
        //               1000
        //               ));
        //    EngineDataProxy<int>.Instance.AddWorldAction(
        //           new EngineDataProxy<int>.WorldAction(
        //               typeof(PeasantDataProxy),
        //               PeasantDataProxy.Action,
        //               500
        //               ));
        //}
    }
}
